package com.sesame.controller;

import kim.sesame.framework.encryption.DES3Utils;
import kim.sesame.framework.entity.GMap;
import kim.sesame.framework.exception.BusinessException;
import kim.sesame.framework.utils.StringUtil;
import kim.sesame.framework.web.context.SpringContextUtil;
import kim.sesame.framework.web.controller.AbstractWebController;
import kim.sesame.framework.web.response.Response;
import kim.sesame.framework.web.util.CookieUtil;
import lombok.extern.apachecommons.CommonsLog;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 用户信息
 *
 * @author wangjianghai
 * @date 2017年5月11日 下午9:51:05
 * @Description: 主要用户保存用户数据源信息 cookie
 * http://www.cnblogs.com/muzongyan/archive/2010/08/30/1812552.html
 */
@CommonsLog
@Controller
@RequestMapping("/user")
public class UserController extends AbstractWebController {

    static String ENCODE = "UTF-8";

    /**
     * 保存json到cookie
     *
     * @param request
     * @param response
     * @param jsonfile
     * @return
     */
    @RequestMapping("/jsonfile")
    @ResponseBody
    public Response jsonfile(HttpServletRequest request, HttpServletResponse response, MultipartFile file) throws Exception {
        if (file.getSize() > 0) {
            String str = new String(file.getBytes());
            JSONArray jsonArr = JSONArray.fromObject(str);

            String newstr = DES3Utils.encodeCBC(jsonArr.toString());

            CookieUtil.addCookie(response, "db_json", newstr, 30 * 24 * 60 * 60);

            return returnSuccess(jsonArr);
        } else {
            throw new BusinessException("上传的文件为空");
        }
    }

    /**
     * 获取数据源的json信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getjosn")
    @ResponseBody
    public Response getjosn(HttpServletRequest request, HttpServletResponse response) {
        JSONArray jsonArr = new JSONArray();
        Cookie coo = CookieUtil.getCookieByName(request, "db_json");
        if (coo != null) {
            String str = coo.getValue();
            if (StringUtil.isNotEmpty(str)) {
                jsonArr = JSONArray.fromObject(DES3Utils.decodeCBC(str));
                //jsonArr.toJson(str);
            }
        }
        return returnSuccess(jsonArr);
    }

    @RequestMapping("/downloadDbTem")
    public void downloadDbTem(HttpServletRequest request, HttpServletResponse response) {

        String basePath = SpringContextUtil.getCurrentPath();
        try {

            // 实现文件下载
            response.setContentType("text/plain");
            response.setHeader("Location", "databases.json");
            response.setHeader("Content-Disposition", "attachment; filename=databases.json");
            OutputStream outputStream = response.getOutputStream();
            InputStream inputStream = new FileInputStream(basePath + "/theme/databases.json");

            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            outputStream.write(b);

            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
